#include "StdAfx.h"
#include ".\id3tags.h"

CID3Tags::CID3Tags(ID3_Tag *tag)
: theTag(tag)
{
}

CID3Tags::~CID3Tags(void)
{
}


#define BUFFER_SIZE 1024
CString CID3Tags::getTag(ID3_FrameID frameID, ID3_FieldID fieldID) {
	ID3_Frame* myFrame;
	ID3_Field* myField;

	char buffer [BUFFER_SIZE];
	memset(buffer, 0, sizeof(buffer));

	if ((myFrame = theTag->Find(frameID)) != NULL)
		if ((myField = myFrame->GetField(fieldID)) != NULL)
			myField->Get(buffer, BUFFER_SIZE);
	CString result;
	result = buffer;
	return result;
}

int CID3Tags::getTags(CString fileName)
{
	if (theTag == NULL || fileName.IsEmpty()) {
		_trackNumData = "";	// Track
		_titleData = "";	// Title
		_artistData = "";	// Artist
		_albumData = "";	// Album
		_yearData = "";	// Year
		_copyrightData = "";	// Copyright
		_urlData = ""; // URL
		_encodedByData = ""; // Encoded By
		_genreData = ""; // Genre
		_commentData = "";	// Comment
		return 0;
	}
	_trackNumData = getTag(ID3FID_TRACKNUM, ID3FN_TEXT);	// Track
	_trackNumData = getTag(ID3FID_TRACKNUM, ID3FN_TEXT);	// Track
	_titleData = getTag(ID3FID_TITLE, ID3FN_TEXT);	// Title
	_titleData = getTag(ID3FID_TITLE, ID3FN_TEXT);	// Title
	_artistData = getTag(ID3FID_LEADARTIST, ID3FN_TEXT);	// Artist
	_artistData = getTag(ID3FID_LEADARTIST, ID3FN_TEXT);	// Artist
	_albumData = getTag(ID3FID_ALBUM, ID3FN_TEXT);	// Album
	_albumData = getTag(ID3FID_ALBUM, ID3FN_TEXT);	// Album
	_yearData = getTag(ID3FID_YEAR, ID3FN_TEXT);	// Year
	_yearData = getTag(ID3FID_YEAR, ID3FN_TEXT);	// Year
	_copyrightData = getTag(ID3FID_COPYRIGHT, ID3FN_TEXT);	// Copyright
	_copyrightData = getTag(ID3FID_COPYRIGHT, ID3FN_TEXT);	// Copyright
	_urlData = getTag(ID3FID_WWWUSER, ID3FN_URL); // URL
	_urlData = getTag(ID3FID_WWWUSER, ID3FN_URL); // URL
	_encodedByData = getTag(ID3FID_ENCODEDBY, ID3FN_TEXT); // Encoded By
	_encodedByData = getTag(ID3FID_ENCODEDBY, ID3FN_TEXT); // Encoded By
	_genreData = getTagGenre(); // Genre
	_genreData = getTagGenre(); // Genre
	_commentData = getTag(ID3FID_COMMENT, ID3FN_TEXT);	// Comment
	_commentData = getTag(ID3FID_COMMENT, ID3FN_TEXT);	// Comment
	return 0;
}

#define UNKNOWN_GENRE 126
CString CID3Tags::getTagGenre(void)
{
	static char* genreList[] = {
		"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal", 
			"New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", 
			"Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", 
			"Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", 
			"Alt. Rock", "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", 
			"Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta Rap", 
			"Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave", "Psychedelic", "Rave", "Showtunes", 
			"Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", 
			"Folk", "Folk/Rock", "National Folk", "Swing", "Fast-Fusion", "Bebob", "Latin", "Revival", "Celtic", 
			"Bluegrass", "Avantgarde", "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band", "Chorus", 
			"Easy Listening", "Acoustic", "Humour", "Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", 
			"Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", 
			"Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo", "A Cappella", "Euro-House", "Dance Hall", "Unknown"
	};
	CString tag = getTag(ID3FID_CONTENTTYPE, ID3FN_TEXT); // Genre
	if (tag[0] == '(') { 
		tag.Remove('(');
		tag.Remove(')');
		int item = atoi(tag);
		if (item >= 0 && item < UNKNOWN_GENRE)
			tag = genreList[item];
		else 
			tag = genreList[UNKNOWN_GENRE];
	}
	else
		tag = getTag(ID3FID_CONTENTTYPE, ID3FN_TEXT);
	return tag;
}